package com.checkchan.ethshare.controller;

import com.checkchan.ethshare.model.ComResponse;
import com.checkchan.ethshare.service.share.ShareService;
import com.checkchan.ethshare.service.share.ShareServiceImp;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.web3j.crypto.CipherException;

import java.io.IOException;

@RestController
@RequestMapping(value = "/share")
public class ShareController {

    @Autowired
    private ShareService shareService;

    @RequestMapping(value = "/getShares", method = RequestMethod.GET)
    @ApiOperation(value = "获取所有分享活动(分页)",httpMethod = "GET")
    public ComResponse getShares(@ApiParam(value = "页码", required = false) @RequestParam("pageIndex") int pageIndex,
                                 @ApiParam(value = "页数大小", required = false) @RequestParam("pageSize") int pageSize) {
        pageSize = pageSize == 0 ? 20 : pageSize;

        ComResponse res = new ComResponse();
        res.setSuccess(true);
        res.setCode(200);
        res.setMessage("操作成功");
        res.setData(shareService.getShares(pageIndex, pageSize));

        return res;
    }

    @RequestMapping(value = "/getConferees/{shareIndex}", method = RequestMethod.GET)
    @ApiOperation(value = "获取所有评分记录",httpMethod = "GET")
    public ComResponse getConferees(@ApiParam(value = "分享活动id", required = true) @PathVariable int shareIndex) {
        ComResponse res = new ComResponse();
        res.setSuccess(true);
        res.setCode(200);
        res.setMessage("操作成功");
        res.setData(shareService.getConfereeInfo(shareIndex));

        return res;
    }

    @RequestMapping(value = "/raiseShare", method = RequestMethod.POST)
    @ApiOperation(value = "发起一项分享",httpMethod = "POST")
    public ComResponse raiseShare(@ApiParam(value = "描述分享", required = true) @RequestParam("desc") String desc) {

        ComResponse res = new ComResponse();
        res.setSuccess(true);
        res.setCode(200);
        res.setMessage("操作成功");

        shareService.raiseShare(desc);

        return res;
    }

    @RequestMapping(value = "/gradeShare", method = RequestMethod.POST)
    @ApiOperation(value = "对分享进行评分",httpMethod = "POST")
    public ComResponse gradeShare(@ApiParam(value = "账号的密码", required = true) @RequestParam("password") String password,
                                  @ApiParam(value = "评分分数", required = true) @RequestParam("score") int score,
                                  @ApiParam(value = "分享活动id", required = true) @RequestParam("shareIndex") int shareIndex) {

        ComResponse res = new ComResponse();
        res.setSuccess(true);
        res.setCode(200);
        res.setMessage("操作成功");

        try {
            ShareService gradeShareService = new ShareServiceImp(password);
            gradeShareService.gradeShare(shareIndex,score);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (CipherException e) {
            e.printStackTrace();
        }
        return res;
    }

}

